<?php

namespace app\stores\logic;


use app\beautiful\model\mall\FlashSaleGoodsModel;

use app\beautiful\model\mall\GoodsModel;

use app\beautiful\model\mall\PinTuanGoodsModel;
use app\common\model\ShopDetailModel;
use app\common\model\ShopModel;
use app\stores\model\order\GoodsOrderModel;

class ConDataLogic{


    protected $today;

    public function __construct()

    {

        $this->today=date('Y-m-d', time());

    }



    public function getData(array $param)

    {

        $datas=[];

        switch ($param['key']){

            case 'coupon':

                $datas[$param['key']]=$this->getCouponData($param['num']);

                break;

            case 'miaosha' :// 秒杀

                $datas[$param['key']]=$this->getFlashSaleData($param['num'],$param);

                break;

            case 'booking' ://预约

                $datas[$param['key']]=$this->getPreSaleData($param['num']);

                break;

            case 'pintuan' : //拼团

                $datas[$param['key']]=$this->getPinTuanData($param['num'],$param);

                break;

            case 'store' :

                $datas[$param['key']]=$this->getStoreData($param['num']);

                break;

            case 'product' : //推荐的产品

                $datas[$param['key']]=$this->getTuiGoodsData($param);

                break;

            case 'tuan'://抢购

                $datas[$param['key']]=$this->getSnapUpData($param['num']);

                break;

            case 'oncecard':

                $datas[$param['key']]=$this->getOncecardData($param['num']);

                break;

            case 'look':

                $datas[$param['key']] = $this->getLookData($param['num']);

                break;

            case 'project':

                $datas[$param['key']]=$this->getProjectData($param['num']);

                break;

        }

        return $datas;

    }

    protected function getFlashSaleData($limit,$param)

    {

        $where=[

            ['status','=',1],

            ['quota','>',0],

            ['is_delete','=',0],

            ['start_time','<=',$this->today],

            ['end_time','>=',$this->today],

        ];
        //存在的门店
        $shop_ids = ShopModel::where(['open_status'=>1])->column('shop_id');
        $where[] = ['shop_id','In',$shop_ids];
        //获取可用的商品id集合

        $goods_where=[

            ['status','=',1],

            ['is_delete','=',0],

            ['is_open_yu','=',0],

            ['online_sales','=',1],
        ];

        $goods_ids=GoodsModel::where($goods_where)->column('goods_id') ?? [];

        $where[]=['goods_id','In',$goods_ids];

        if(isset($param['shop_id']) && $param['shop_id']>0) $where[]=['shop_id','=',$param['shop_id']];

        if(isset($param['city']) && $param['city']){

            $shop_ids = ShopDetailModel::where(['city'=>$param['city']])->column('shop_id')?? [];

            if($shop_ids) $where[] = ['shop_id','In',$shop_ids];
        }

        $order=['is_tui'=>'desc','fictitious_take_count'=>'desc','flash_sale_price'=>'asc'];

        $datas=FlashSaleGoodsModel::with(['goods'=>function($query){

            $query->field('goods_id,name,original_price,total_value,cover_img,type');

        }])->where($where)

            ->order($order)

            ->field('shop_id,flash_sale_goods_id,goods_id,flash_sale_price,fictitious_take_count,quota,end_time,take_count,limit_num_type')

            ->limit($limit)

            ->select()

            ->append(['flash_sale_djs'])

            ->toArray();

        foreach ($datas as $k=>$v){
            //每天限量
            if($v['limit_num_type']==1){
                //每天的订单
                $s = strtotime(date('Y-m-d').'00:00:00');
                $l = strtotime(date('Y-m-d').'23:59:59');
                $today_num = GoodsOrderModel::where(['flash_sale_goods_id'=>$v['flash_sale_goods_id'],'is_paid'=>1,'is_delete'=>0,'from_type'=>2])->whereIn('add_time',[$s,$l])->count();
                $datas[$k]['flash_sale_progress'] = (round($today_num/$v['quota'],2)*100)."%";
            }else{
                //全部库存
                $datas[$k]['flash_sale_progress'] = ($v['fictitious_take_count']+$v['quota'])<1?'100%':(round($v['fictitious_take_count']/($v['fictitious_take_count']+$v['quota']),2)*100)."%";
            }
            $datas[$k]['end_time'] = $v['end_time'].' 23:59:59';

        }
        return $datas;

    }

    protected function getPinTuanData($limit,$param)

    {

        $where=[

            ['status','=',1],

            ['is_delete','=',0],

            ['start_time','<=',$this->today],

            ['end_time','>=',$this->today],

        ];
        //存在的门店
        $shop_ids = ShopModel::where(['open_status'=>1])->column('shop_id');
        $where[] = ['shop_id','In',$shop_ids];

        //获取可用的商品id集合

        $goods_where=[

            ['status','=',1],

            ['is_delete','=',0],

            ['is_open_yu','=',0],

            ['online_sales','=',1],

        ];

        $goods_ids=GoodsModel::where($goods_where)->column('goods_id') ?? [];

        $where[]=['goods_id','in',$goods_ids];

        if(isset($param['shop_id']) && $param['shop_id']>0) $where[]=['shop_id','=',$param['shop_id']];

        if(isset($param['city']) && $param['city']){

            $shop_ids = ShopDetailModel::where(['city'=>$param['city']])->column('shop_id')?? [];

            if($shop_ids) $where[] = ['shop_id','In',$shop_ids];
        }


        $order=['is_tui'=>'desc','fictitious_take_count'=>'desc','pin_tuan_price'=>'asc'];

        $datas=PinTuanGoodsModel::with(['goods'=>function($query){

            $query->field('goods_id,name,original_price,total_value,cover_img,type');

        }])->where($where)

            ->order($order)

            ->field('shop_id,pin_tuan_goods_id,group_num,goods_id,pin_tuan_price,fictitious_take_count,quota,end_time')

            ->limit($limit)

            ->select()

            ->append(['pin_tuan_progress'])

            ->toArray();

        foreach ($datas as $k=>$v){
            $datas[$k]['end_time'] = $v['end_time'].' 23:59:59';
        }
        return $datas;

    }






}

